#-*- coding: utf-8 -*-
import tensorflow as tf
import pickle


def get_batch_data(file_list, batch_size=10, epoch=1):
    """
    get shuffle batch data_prepare
    """
    queue = tf.train.string_input_producer(file_list, num_epochs=epoch)
    reader = tf.TFRecordReader()
    _, serialized_example = reader.read(queue)
    batch = tf.train.shuffle_batch(
        tensors=[serialized_example],
        batch_size=batch_size,
        capacity=batch_size*10 + 100,
        num_threads=10,
        min_after_dequeue=100
    )
    data = tf.parse_example(
        serialized=batch,
        features={
            "label": tf.FixedLenFeature([1], tf.int64),
            "item": tf.FixedLenFeature([50], tf.int64),
            "weight": tf.FixedLenFeature([50], tf.float32),
            "other": tf.FixedLenFeature([13], tf.float32)
        }
    )
    return data


def get_total_data(file_path):
    record_total = sum(1 for _ in tf.python_io.tf_record_iterator(file_path))
    data = get_batch_data([file_path], batch_size=record_total / 4, epoch=1)
    return data


def get_dict(path):
    pkl_file = open(path, 'rb')
    item_dict = pickle.load(pkl_file)
    pkl_file.close()
    return item_dict


def find_key_by_value(item_dict, values):
    key = []
    for value in values:
        key.append([list(item_dict.keys())[list(item_dict.values()).index(x)] for x in value])
    return key


def find_value_by_key(item_dict, key):
    values = [item_dict[x] for x in key]
    return values